package ch.zbw.pra.sysventory.utils;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Umwandlungen von Date, DateTime, Timestamp Felder auf das gewünscht Format
 * 
 * @author Ralph Koster
 */

public class DateUtils {
	
	private static final Logger LOG = LoggerFactory.getLogger(DateUtils.class);

	public static String getCurrentDatetimeFormatted() {
		DateTime date = new DateTime();
		return date.toString("dd.MM.yyyy 'um' HH:mm:ss");
	}
	
	public static Timestamp getCurrentSqlTimeStamp() {
		DateTime today = new DateTime();
		return new Timestamp(today.getMillis());
	}

	public static Timestamp getSqlTimestampForDate(DateTime date) {
		return new Timestamp(date.getMillis());
	}
	
	public static Timestamp convertLocalDateToTimestamp(LocalDate localDate) {
		Timestamp timestamp;
		timestamp = new Timestamp(localDate.toDateMidnight().getMillis());
		return timestamp;
	}

	public static String calculateDurationFromTimestamps(Timestamp scanStart,
			Timestamp scanEnd) {
		long difference = scanEnd.getTime() - scanStart.getTime();
		long hours = difference/3600000;
		long minutes = difference/60000 % 60;
		long seconds = difference/1000 % 60;
		String duration = String.format("%02d:%02d:%02d", hours, minutes, seconds);
		return duration;
	}

	public static HashMap<String, LocalDate> getCurrentYearRange() {
		LocalDate yearBegin = new LocalDate().withDayOfYear(1);
		LocalDate yearEnd = new LocalDate().withDayOfYear(1).plusMonths(12).minusDays(1);
		HashMap<String, LocalDate> yearRange = new HashMap<>();
		yearRange.put("dateFrom", yearBegin);
		yearRange.put("dateTo", yearEnd);
		LOG.debug("Year begin: " + yearBegin);
		LOG.debug("Year end: " + yearEnd);
		return yearRange;
	}

	public static HashMap<String, LocalDate> getCurrentMonthRange() {
		LocalDate monthBegin = new LocalDate().withDayOfMonth(1);
		LocalDate monthEnd = new LocalDate().plusMonths(1).withDayOfMonth(1).minusDays(1);
		HashMap<String, LocalDate> monthRange = new HashMap<>();
		monthRange.put("dateFrom", monthBegin);
		monthRange.put("dateTo", monthEnd);
		LOG.debug("Month begin: " + monthBegin);
		LOG.debug("Month end: " + monthEnd);
		return monthRange;
	}

	public static HashMap<String, LocalDate> getCurrentWeekRange() {
		LocalDate weekBegin = new LocalDate().withDayOfWeek(1);
		LocalDate weekEnd = new LocalDate().plusWeeks(1).withDayOfWeek(1);
		HashMap<String, LocalDate> weekRange = new HashMap<>();
		weekRange.put("dateFrom", weekBegin);
		weekRange.put("dateTo", weekEnd);
		LOG.debug("Week begin: " + weekBegin);
		LOG.debug("Week end: " + weekEnd);
		return weekRange;
	}

	public static LocalDate convertStringToLocalDate(String date) {
		LocalDate localDate = LocalDate.parse(date);
		return localDate;
	}

	public static LocalDate convertDateToLocalDate(Date date) {
		LocalDate localDate = new DateTime(date).toLocalDate();
		return localDate;
	}
	
	public static String convertTimestampToDate(Timestamp timestamp) {
		DateTime date = new DateTime(timestamp.getTime());
		return date.toString("dd.MM.yyyy");
	}
	
	public static String convertTimestampToDateTime(Timestamp timestamp) {
		LocalDateTime date = new LocalDateTime(timestamp.getTime());
		return date.toString("dd.MM.yyyy 'um' HH:mm:ss");
	}
}
